<template>
<div id='denglu'>
     <div class="mainbody">
       <el-button class="returnLast" icon="el-icon-back" @click="returnLast">返回上一级</el-button>
       <el-form ref="form" :model="form" label-width="80px">
        <el-form-item label="账号：">
        <el-input  v-model="form.username"  placeholder="请输入账号"></el-input>
        </el-form-item>
        <el-form-item label="密码：">
        <el-input  v-model="form.password" placeholder="请输入密码" show-password></el-input>
        </el-form-item>
         <el-form-item label="密码：">
        <el-input  v-model="form.password2" placeholder="请再次输入密码" show-password></el-input>
        </el-form-item>
         <el-form-item>
          <el-button type="primary" @click="register">注册</el-button>
          </el-form-item>
      </el-form>
   </div>
</div>
</template>

<script>
export default {
  name: 'HomeRegister',
  data () {
    return {
      form: {
        username: '',
        password: '',
        password2: ''
      }
    }
  },
  methods: {
    register: function () {
      if (this.form.username === '' || this.form.password === '' || this.form.password2 === '') {
        this.$message('请填写完整')
        return
      }
      if (this.form.password !== this.form.password2) {
        this.$message('两次密码输入不对')
        return
      }
      let that = this
      this.$http({'userName': this.form.username, 'userPassword': this.form.password}, '/api/user/register').then(function (response) {
        if (response.status === 200) {
          that.$message('注册成功，请登录')
          that.$router.push('/')
        }
      }).catch(function (response) {
        that.$message('该账号已被注册，请换账号')
      })
    },
    returnLast: function () {
      this.$router.push('/')
    }
  }
}
</script>

<style scoped>
.mainbody{
  position: absolute;
  left: 50%;
  top: 50%;
  width: 350px;
  height: 330px;
  margin: -150px 0 0 -190px;
  padding: 20px 30px 30px 30px;
  border-radius: 5px;
  background: #fff;
}
.register{
  font-size: 25px;
  text-align: center;
}
.el-icon-back{
  float: left;
  font-size: 20px;
  position: absolute;
  top:15px;
  left: 10px;
  color: green;
  cursor: pointer;
}
.returnLast{
  margin: 0px 0px 30px 0px;
}
</style>
